class 碰撞器 {
  constructor() {}

  isCollide(a, b) {
    const aLeft = a.x;
    const aLeftFull = aLeft + a.width;
    const aTop = a.y;
    const aTopFull = aTop + a.height;

    const bLeft = b.x;
    const bLeftFull = bLeft + b.width;
    const bTop = b.y;
    const bTopFull = bTop + b.height;

    const isXCollide = aLeftFull > bLeft && aLeft < bLeftFull;
    const isYCollide = aTopFull > bTop && aTop < bTopFull;

    if (isXCollide && isYCollide) {
      return true;
    }

    return false;
  }
}
const collide = new 碰撞器();

function Boom(target) {
  const boom = document.createElement("div");
  boom.style.left = target.x + "px";
  boom.style.top = target.y + "px";
  document.getElementById("main").appendChild(boom);
  boom.classList.add("boom");
  setTimeout(() => boom.remove(), 300);
}

const Game = {
  width: window.innerWidth,
  height: window.innerHeight,
  grid: 10,
  gameFrameIndex: 0,
  players: [],
  cpus: [],
  // 正在飞行的子弹: [],
  正在飞行的子弹_电脑: [],
  正在飞行的子弹_玩家: [],
  levels: JSON.parse(JSON.stringify(levels)),
  currentLevel: 0,
  canvas: document.getElementById("canvas"),
  ctx: null,
  renderScores() {
    this.players.map((player, index) => {
      const 分数 = player.分数;
      const id = "player-score-" + index;
      const queryDom = document.getElementById(id);

      const text = `[玩家${index + 1}] 分数:${Math.floor(
        分数
      )},所需升级分数:${Math.floor(player.所需升级分数)},生命值:${
        player.飞机.hp
      },攻击力:${player.飞机.power},等级:${player.level}`;

      if (queryDom) {
        queryDom.innerText = text;
      } else {
        const dom = document.createElement("div");
        dom.setAttribute("id", id);
        dom.setAttribute("class", "score-count");
        dom.innerText = text;
        document.getElementById("scores").appendChild(dom);
      }
    });
  },
  init() {
    this.canvas.style.width = this.width + "px";
    this.canvas.style.height = this.height + "px";
    this.canvas.setAttribute("width", this.width);
    this.canvas.setAttribute("height", this.height);
    this.ctx = this.canvas.getContext("2d");
  },
  updateBiu() {
    this.正在飞行的子弹_玩家 = this.正在飞行的子弹_玩家.filter((bullet) => {
      if (!bullet.isDestroy) {
        const rotate = bullet.target.rotate;
        if (rotate === 180) {
          bullet.move(bullet.x, bullet.y + bullet.speed);
        } else {
          bullet.move(bullet.x, bullet.y - bullet.speed);
        }

        bullet.render();
      }

      return !bullet.isDestroy;
    });
    this.正在飞行的子弹_电脑 = this.正在飞行的子弹_电脑.filter((bullet) => {
      if (!bullet.isDestroy) {
        const rotate = bullet.target.rotate;
        if (rotate === 180) {
          bullet.move(bullet.x, bullet.y + bullet.speed);
        } else {
          bullet.move(bullet.x, bullet.y - bullet.speed);
        }

        bullet.render();
      }

      return !bullet.isDestroy;
    });
  },
  updateCollide() {
    this.cpus = this.cpus.filter((cpu) => {
      if (!cpu.isDestroy) {
        this.正在飞行的子弹_玩家.forEach((zidan) => {
          if (collide.isCollide(cpu.飞机, zidan) && !zidan.isDestroy) {
            cpu.飞机.beHit(zidan.target.target);
            if (!zidan.是否穿透) {
              zidan.destroy();
            }
          }
        });

        this.players.forEach((player) => {
          if (player.飞机.保护中 <= 0) {
            if (collide.isCollide(player.飞机, cpu.飞机)) {
              player.飞机.beHit(cpu);
              player.飞机.触发保护(100);
            }

            this.正在飞行的子弹_电脑.forEach((zidan) => {
              if (collide.isCollide(player.飞机, zidan) && !zidan.isDestroy) {
                player.飞机.beHit(cpu);
                player.飞机.触发保护(100);
                zidan.destroy();
              }
            });
          }
        });
      }

      return !cpu.isDestroy;
    });

    // this.players.forEach((player) => {
    //   this.正在飞行的子弹_电脑.forEach((zidan) => {
    //     if (collide.isCollide(player.飞机, zidan)) {
    //       player.飞机.beHit(cpu);
    //       player.飞机.触发保护(100);
    //     }
    //   });

    //   // this.cpus.forEach((cpu) => {
    //   //   if (player.飞机.保护中 <= 0) {
    //   //     if (collide.isCollide(player.飞机, cpu.飞机)) {
    //   //       player.飞机.beHit(cpu);
    //   //       player.飞机.触发保护(100);
    //   //     }
    //   //   }

    //   //   // this.正在飞行的子弹.forEach((zidan) => {
    //   //   //   if (
    //   //   //     collide.isCollide(zidan, cpu.飞机) &&
    //   //   //     !(zidan.target.target instanceof 电脑)
    //   //   //   ) {
    //   //   //     zidan.destroy();
    //   //   //     cpu.飞机.beHit(cpu);
    //   //   //   } else if (
    //   //   //     collide.isCollide(zidan, player.飞机) &&
    //   //   //     !(zidan.target.target instanceof 玩家)
    //   //   //   ) {
    //   //   //     zidan.destroy();
    //   //   //     player.飞机.beHit(cpu);
    //   //   //     player.飞机.触发保护(100);
    //   //   //   }
    //   //   // });

    //   //   // cpu.飞机.正在飞行的子弹.forEach((biu) => {
    //   //   //   if (collide.isCollide(biu, player.飞机)) {
    //   //   //     biu.destroy();
    //   //   //     player.飞机.beHit(cpu);
    //   //   //     player.飞机.触发保护(100);
    //   //   //   }
    //   //   // });
    //   //   // player.飞机.正在飞行的子弹.forEach((biu) => {
    //   //   //   if (collide.isCollide(biu, cpu.飞机)) {
    //   //   //     biu.destroy();
    //   //   //     cpu.飞机.beHit(player);
    //   //   //     if (cpu.飞机.hp <= 0) {
    //   //   //       // Boom(cpu.飞机);
    //   //   //     }
    //   //   //     mp3.被击中.play();
    //   //   //   }
    //   //   // });
    //   // });
    // });
  },
  update() {
    Game.gameFrameIndex++;
    this.renderScores();
    this.updateBiu();
    this.updateCollide();
  },
};

const Loader = {
  imgs: {
    // "/xxx.png|width|height": Img
  },
  get(src, width, height) {
    const key = `${src}|${width}|${height}`;
    if (this.imgs[key]) {
      return Promise.resolve(this.imgs[key]);
    }

    return new Promise((resolve) => {
      const img = new Image();
      img.style.width = width + "px";
      img.style.height = height + "px";
      img.onload = () => {
        resolve(img);
      };
      img.src = src;
      this.imgs[key] = img;
    });
  },
};

Game.init();
// document.getElementById("main").style.width = Game.width + "px";
// document.getElementById("main").style.height = Game.height + "px";

class 音频 {
  constructor() {
    this.子弹发射 = new Howl({
      src: ["./assets/mp3/bullet.mp3"],
      volume: 0.5,
    });
    this.背景音乐 = new Howl({
      src: ["./assets/mp3/game_music.mp3"],
      autoplay: true,
      loop: true,
    });
    this.敌人死亡 = new Howl({
      src: ["./assets/mp3/enemy1_down.mp3"],
      volume: 1,
    });
    this.敌人进场2 = new Howl({
      src: ["./assets/mp3/enemy2_down.mp3"],
      volume: 0.5,
    });
    this.被击中 = new Howl({
      src: ["./assets/mp3/enemy3_down.mp3"],
      volume: 1,
    });
    this.敌人发射子弹2 = new Howl({
      src: ["./assets/mp3/enemy4_down.mp3"],
      volume: 0.3,
    });
    this.敌人进场5 = new Howl({
      src: ["./assets/mp3/enemy5_down.mp3"],
      volume: 0.5,
    });
    this.敌人离场1 = new Howl({
      src: ["./assets/mp3/enemy2_out.mp3"],
      volume: 0.5,
    });
    this.保护中 = new Howl({
      src: ["./assets/mp3/enemy4_out.mp3"],
      volume: 0.5,
    });
  }
}

const mp3 = new 音频();
mp3.背景音乐.play();

class 电脑 {
  constructor(configs = {}) {
    this.分数 = configs.分数 ?? 10;
    this.飞机 = null;
    this.攻击间隔 = configs.攻击间隔 ?? 10; // 0等于不自动发射子弹
    this.限制发射计数 = 0;
    this.isDestroy = false;
  }
  onChangeScore() {}
  // 绑定飞机
  绑定飞机(飞机) {
    this.飞机 = 飞机;
    this.飞机.setTarget(this);
    this.飞机.create(0, 0);
    return this;
  }

  自动移动() {
    this.飞机.move(this.飞机.x, (this.飞机.y += this.飞机.speed));
    this.飞机.render();
    if (this.飞机.y > Game.height) {
      this.destroy();
    }
  }

  自动攻击() {
    if (this.攻击间隔 !== 0) {
      if (this.限制发射计数 >= this.攻击间隔) {
        this.飞机.biu();
        mp3.敌人发射子弹2.play();
        this.限制发射计数 = 0;
      }
      this.限制发射计数++;
    }
  }

  die() {
    this.destroy();
    mp3.敌人死亡.play();
  }

  destroy() {
    this.飞机.destroy();
    this.isDestroy = true;
  }

  update() {
    if (!this.isDestroy) {
      this.自动攻击();
      this.自动移动();
    }
  }
}

class 玩家 {
  constructor(configs) {
    this.飞机 = null;
    this.分数 = 0;
    this.所需升级分数 = 100;
    this.level = 1;
    this.levelUpdating = false;

    this.按键配置 = configs.按键配置 || {
      上: "KeyW",
      下: "KeyS",
      左: "KeyA",
      右: "KeyD",
      发射: "KeyJ",
    };

    this.按键 = {
      上: false,
      下: false,
      左: false,
      右: false,
      发射: false,
    };
  }
  // 控制飞机移动
  控制飞机移动() {
    // 监听键盘
    document.addEventListener("keydown", (event) => {
      const key = event.code;
      switch (key) {
        case this.按键配置.左:
          this.按键.左 = true;
          break;
        case this.按键配置.右:
          this.按键.右 = true;
          break;
        case this.按键配置.上:
          this.按键.上 = true;
          break;
        case this.按键配置.下:
          this.按键.下 = true;
          break;
        case this.按键配置.发射:
          this.按键.发射 = true;
          break;
      }
    });

    document.addEventListener("keyup", (event) => {
      const key = event.code;

      switch (key) {
        case this.按键配置.左:
          this.按键.左 = false;
          break;
        case this.按键配置.右:
          this.按键.右 = false;
          break;
        case this.按键配置.上:
          this.按键.上 = false;
          break;
        case this.按键配置.下:
          this.按键.下 = false;
          break;
        // 发射子弹
        case this.按键配置.发射:
          this.按键.发射 = false;
          break;
      }
    });
  }
  // 绑定飞机
  绑定飞机(飞机) {
    this.飞机 = 飞机;
    this.飞机.setTarget(this);
    this.飞机.create();
  }
  onChangeScore() {
    if (this.分数 > this.所需升级分数) {
      this.level++;
      this.所需升级分数 =
        this.所需升级分数 + this.所需升级分数 * this.level * 0.1;
    }

    if (this.level >= 1) {
      this.飞机.更新配置({
        power: this.level * 2,
        发射数量: 1,
        发射间隔: 8,
      });
    }

    if (this.level >= 5) {
      this.飞机.更新配置({
        power: this.level * 2,
        发射间隔: 9.5,
        发射数量: 2,
        发射偏移: 1,
      });
    }

    if (this.level >= 10) {
      this.飞机.更新配置({
        power: this.level * 1,
        发射间隔: 10,
        发射数量: 3,
        发射偏移: 2,
      });
    }

    if (this.level >= 15) {
      this.飞机.更新配置({
        power: this.level * 1,
        发射间隔: 12,
        发射间隔: 9,
        发射数量: 4,
        发射偏移: 1,
        随机偏移: 2,
      });
    }

    if (this.level >= 20) {
      this.飞机.更新配置({
        power: this.level * 0.1,
        发射间隔: 10,
        发射间隔: 8,
        发射数量: 4,
        发射偏移: 1,
        随机偏移: 2,
        子弹配置: {
          穿透率: 0.5,
        },
      });
    }

    if (this.level >= 25) {
      this.飞机.更新配置({
        power: this.level * 0.1,
        发射间隔: 8,
        发射数量: 5,
        发射偏移: 1,
        随机偏移: 2,
        子弹配置: {
          穿透率: 0.6,
        },
      });
    }

    if (this.level >= 30) {
      this.飞机.更新配置({
        power: this.level * 0.2,
        发射间隔: 6,
        发射数量: 6,
        发射偏移: 1,
        随机偏移: 2,
        子弹配置: {
          穿透率: 0.7,
        },
      });
    }

    if (this.level >= 35) {
      this.飞机.更新配置({
        发射间隔: 5,
        发射数量: 8,
        发射偏移: 1,
        随机偏移: 1,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.5,
        },
      });
    }

    if (this.level >= 40) {
      this.飞机.更新配置({
        发射间隔: 4,
        发射数量: 10,
        发射偏移: 1,
        随机偏移: 1,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.6,
        },
      });
    }

    if (this.level >= 45) {
      this.飞机.更新配置({
        发射间隔: 3,
        发射数量: 20,
        发射偏移: 0.1,
        随机偏移: 0.1,
        power: this.level * 0.08,
        子弹配置: {
          穿透率: 0.1,
        },
      });
    }

    if (this.level >= 50) {
      this.飞机.更新配置({
        发射间隔: 2,
        发射数量: 25,
        发射偏移: 0.1,
        随机偏移: 0.1,
        power: this.level * 0.03,
        子弹配置: {
          穿透率: 0.2,
        },
      });
    }

    if (this.level >= 55) {
      this.飞机.更新配置({
        发射间隔: 1,
        发射数量: 10,
        发射偏移: 0.01,
        随机偏移: 0.01,
        power: this.level * 0.05,
        子弹配置: {
          穿透率: 0.1,
        },
      });
    }

    if (this.level >= 60) {
      this.飞机.更新配置({
        发射间隔: 1,
        发射数量: 30,
        发射偏移: 1,
        随机偏移: 1,
        power: this.level * 0.03,
        子弹配置: {
          穿透率: 0.2,
        },
      });
    }

    if (this.level >= 65) {
      this.飞机.更新配置({
        发射间隔: 5,
        发射数量: 40,
        发射偏移: 0.05,
        随机偏移: 0.05,
        power: this.level * 0.06,
        子弹配置: {
          穿透率: 0.05,
          width: 30,
          height: 60,
        },
      });
    }

    if (this.level >= 70) {
      this.飞机.更新配置({
        发射间隔: 10,
        发射数量: 10,
        发射偏移: 1,
        随机偏移: 1,
        power: this.level * 1,
        子弹配置: {
          穿透率: 0.01,
          width: 50,
          height: 80,
        },
      });
    }

    if (this.level >= 75) {
      this.飞机.更新配置({
        发射间隔: 5,
        发射数量: 30,
        发射偏移: 0.5,
        随机偏移: 10,
        power: this.level * 0.2,
        子弹配置: {
          穿透率: 0.2,
          width: 5,
          height: 8,
        },
      });
    }

    if (this.level >= 80) {
      this.飞机.更新配置({
        发射间隔: 2,
        发射数量: 50,
        发射偏移: 0.5,
        随机偏移: 10,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.2,
          width: 7,
          height: 9,
        },
      });
    }

    if (this.level >= 85) {
      this.飞机.更新配置({
        发射间隔: 1,
        发射数量: 70,
        发射偏移: 0.5,
        随机偏移: 10,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.2,
          width: 10,
          height: 20,
        },
      });
    }

    if (this.level >= 90) {
      this.飞机.更新配置({
        发射间隔: 1,
        发射数量: 80,
        发射偏移: 0.3,
        随机偏移: 8,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.3,
          width: 14,
          height: 28,
        },
      });
    }

    if (this.level >= 100) {
      this.飞机.更新配置({
        发射间隔: 1,
        发射数量: 100,
        发射偏移: 0.3,
        随机偏移: 8,
        power: this.level * 0.1,
        子弹配置: {
          穿透率: 0.3,
          width: 18,
          height: 30,
        },
      });
    }
  }

  die() {}
}

class 飞机 {
  constructor(configs = {}) {
    this.target = null;
    this.img = null;
    this.hp = configs.hp || 10;
    this.power = configs.power ?? 1;
    this.x = configs.x || 0;
    this.y = configs.y || 0;
    this.width = configs.width || 100;
    this.height = configs.height || 100;
    this.speed = configs.speed || 5;
    this.isDestroy = false;
    this.rotate = configs.rotate || 0;
    this.发射间隔 = configs.发射间隔 || 10;
    this.发射数量 = configs.发射数量 || 1;
    this.发射偏移 = configs.发射偏移 || 0;
    this.随机偏移 = configs.随机偏移 || 0;
    this.限制发射计数 = 0;
    this.图片路径 = configs.图片路径;
    // this.正在飞行的子弹 = [];
    this.子弹配置 = configs.子弹配置 || {};
    this.保护中 = 0;
  }

  更新配置(configs = {}) {
    Object.assign(this, configs);
    this.render();
  }

  触发保护(value) {
    this.保护中 = value ?? 1000;
    mp3.保护中.play();
  }

  setTarget(target) {
    this.target = target;
  }

  beHit(target) {
    if (this.保护中 > 0) {
      return;
    }
    this.hp -= target.飞机.power;
    target.分数++;
    if (this.hp <= 0 && !this.isDestroy) {
      this.target.die();
      target.分数 = target.分数 + this.target.分数;
    }

    target.onChangeScore();
  }

  move(x, y) {
    this.y = y;
    this.x = x;
  }

  // 发射器
  biu() {
    for (let i = 0; i < this.发射数量; i++) {
      const bullet = new 子弹({
        rotate: this.rotate,
        ...this.子弹配置,
        target: this,
        偏移:
          (i - (this.发射数量 - 1) / 2) * this.发射偏移 +
          (Math.random() * 1 > 0.5 ? 1 : -1) * this.随机偏移 * Math.random(),
      });

      const x = this.x + this.width / 2 - bullet.width / 2;
      const y =
        this.rotate === 180 ? this.y + this.height : this.y - bullet.height;

      if (this.rotate === 180) {
        bullet.create(x, y);
      } else {
        bullet.create(x, y);
      }

      if (this.target instanceof 电脑) {
        Game.正在飞行的子弹_电脑.push(bullet);
      } else {
        Game.正在飞行的子弹_玩家.push(bullet);
      }
    }
  }

  // 创建飞机
  create() {
    // const img = new Image();
    // img.style.width = this.width + "px";
    // img.style.height = this.height + "px";
    // img.onload = () => {
    //   this.img = img;
    //   this.render();
    // };
    // img.src = this.图片路径;

    Loader.get(this.图片路径, this.width, this.height).then((img) => {
      this.img = img;
      this.render();
    });

    // dom.style.transform = `rotate(${this.rotate}deg)`;
  }

  destroy() {
    this.isDestroy = true;
  }

  render() {
    // 这样会重新计算布局，性能差
    // this.dom.style.left = this.x + "px";
    // this.dom.style.top = this.y + "px";
    if (this.img) {
      Game.ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
    }

    // this.正在飞行的子弹 = this.正在飞行的子弹.filter((item) => !item.isDestroy);
    if (this.保护中 > 0) {
      // this.dom.classList.add("protecting");
      this.保护中--;
    } else {
      // this.dom.classList.remove("protecting");
    }

    // this.updateBiu();
  }
  // die() {

  // }
  // biu() {

  // }
}

class 子弹 {
  constructor(configs = {}) {
    this.x = configs.x || 0;
    this.y = configs.y || 0;
    this.speed = configs.speed || 10; // 子弹发射速度
    this.rotate = configs.rotate || 0; // 子弹方向
    this.src = configs.src || "./assets/image/zidan1.png"; // 子弹发射速度
    this.width = configs.width || 10;
    this.height = configs.height || 40;
    this.偏移 = configs.偏移 || 0;
    this.穿透率 = configs.穿透率 || 0;
    this.是否穿透 = this.穿透率 > Math.random() * 1;
    this.img = null;
    this.isDestroy = false;
    this.target = configs.target;
  }
  // 创建子弹
  create(x, y) {
    this.x = x;
    this.y = y;
    // const dom = document.createElement("img");
    // dom.style.position = "absolute";
    // // dom.style.transform = `rotate(${this.rotate}deg)`;
    // dom.setAttribute("width", this.width);
    // dom.setAttribute("height", this.height);
    // dom.setAttribute("src", this.src);
    // this.dom = dom;

    // const img = new Image();
    // img.style.width = this.width + "px";
    // img.style.height = this.height + "px";
    // img.onload = () => {
    //   this.img = img;
    //   this.render();
    // };
    // img.src = this.src;

    Loader.get(this.src, this.width, this.height).then((img) => {
      this.img = img;
      this.render();
    });

    // document.getElementById("main").appendChild(this.dom);
  }
  render() {
    // this.dom.style.left = this.x + "px";
    // this.dom.style.top = this.y + "px";
    if (this.img) {
      Game.ctx.drawImage(this.img, this.x, this.y, this.width, this.height);
    }
  }
  move(x, y) {
    this.x = x + this.偏移;
    this.y = y;

    if (this.y < -Game.height || this.y > Game.height + this.height) {
      this.destroy();
    }
  }
  destroy() {
    this.isDestroy = true;
  }
}

class 地图 {
  constructor(configs) {
    this.y = 0;
    this.img = null;
    this.src = configs.src || "";
    this.speed = 0.5;
  }

  init() {
    this.img = document.getElementById("canvas");
    this.img.style.backgroundImage = `url(${this.src})`;
    this.img.style.backgroundPositionX = 0;
    this.img.style.backgroundRepeat = "repeat";
    this.img.style.backgroundSize = "100% auto";
  }

  render() {
    this.img.style.backgroundPositionY = this.y + "px";
  }

  move() {
    this.y = this.y + this.speed;
  }
}

const player1 = new 玩家({
  按键配置: {
    上: "KeyW",
    下: "KeyS",
    左: "KeyA",
    右: "KeyD",
    发射: "KeyJ",
  },
});
const feiji1 = new 飞机({
  hp: 10,
  power: 2,
  发射间隔: 30,
  发射数量: 1,
  发射偏移: 0,
  随机偏移: 0,
  图片路径: "./assets/image/飞机3.png",
  子弹配置: {
    width: 10,
    height: 20,
    speed: 10,
    穿透率: 0,
    src: "./assets/image/zidan1.png",
  },
});
feiji1.move((Game.width - feiji1.width) / 2 - feiji1.width, Game.height - 100);
player1.绑定飞机(feiji1);
player1.控制飞机移动();

const player2 = new 玩家({
  按键配置: {
    上: "ArrowUp",
    下: "ArrowDown",
    左: "ArrowLeft",
    右: "ArrowRight",
    发射: "Numpad1",
  },
});
const feiji2 = new 飞机({
  hp: 10,
  power: 20,
  图片路径: "./assets/image/飞机3.png",
  子弹配置: {
    src: "./assets/image/zidan1.png",
    height: 50,
    width: 30,
  },
});
feiji2.move((Game.width - feiji2.width) / 2 + feiji2.width, Game.height - 100);
player2.绑定飞机(feiji2);
player2.控制飞机移动();

const map = new 地图({
  src: "./assets/image/xingyun1.jpg",
});

map.init();

function createCPU(xIndex, yIndex) {
  const cpu = new 电脑({
    攻击间隔: 300 + Math.random() * 500,
    分数: Math.max(player1.分数, player2.分数) * 0.5 || 100,
  });

  const fj = new 飞机({
    图片路径: "./assets/image/飞机1.svg",
    speed: 1,
    rotate: 180,
    hp: Math.max(player1.level, player2.level) * 20,
    power: 1,
    width: 50,
    height: 50,
    子弹配置: {
      speed: 2,
      width: 10,
      height: 20,
    },
  });
  const gridWidth = Game.width / Game.grid;
  fj.move(gridWidth * xIndex, -fj.height);

  cpu.绑定飞机(fj);

  Game.cpus.push(cpu);

  return cpu;
}

const 敌人渲染器 = {
  scenes: [],
  render() {
    this.scenes.forEach((scene) => {
      if (scene.cpus.length) {
        // scene.cpus = scene.cpus.filter((cpu) => !cpu.飞机.isDestroy);
        scene.cpus.forEach((item, xIndex) => {
          if (item === 1) {
            scene.cpus[xIndex] = createCPU(xIndex);
          }
          const cpu = scene.cpus[xIndex];
          if (cpu instanceof 电脑) {
            cpu.update();
          }
        });
      }
    });
  },
};

function playersRender() {
  const players = Game.players;
  players.forEach((player) => {
    if (player.按键.左 == true && player.飞机.x > 0) {
      player.飞机.move(player.飞机.x - player.飞机.speed, player.飞机.y);
    }
    if (
      player.按键.右 == true &&
      player.飞机.x + player.飞机.width <= Game.width
    ) {
      player.飞机.move(player.飞机.x + player.飞机.speed, player.飞机.y);
    }
    if (player.按键.上 == true && player.飞机.y > 0) {
      player.飞机.move(player.飞机.x, player.飞机.y - player.飞机.speed);
    }
    if (
      player.按键.下 == true &&
      player.飞机.y + player.飞机.height <= Game.height
    ) {
      player.飞机.move(player.飞机.x, player.飞机.y + player.飞机.speed);
    }

    player.飞机.限制发射计数++;
    if (player.按键.发射 == true) {
      if (player.飞机.限制发射计数 > player.飞机.发射间隔) {
        player.飞机.biu();
        mp3.子弹发射.play();
        player.飞机.限制发射计数 = 0;
      }
    }
    player.飞机.render();
  });
}
function cpusRender() {
  if (Game.levels[Game.currentLevel]) {
    const currentScene = Game.levels[Game.currentLevel][Game.gameFrameIndex];
    if (currentScene) {
      //  出现一排敌人
      敌人渲染器.scenes.push(currentScene);
    }

    const currentLevelSceneKeys = Object.keys(Game.levels[Game.currentLevel]);
    if (
      Game.gameFrameIndex >
        currentLevelSceneKeys[currentLevelSceneKeys.length - 1] &&
      Game.cpus.length <= 0
    ) {
      // 下一关
      if (Game.levels[Game.currentLevel + 1]) {
        Game.gameFrameIndex = 0;
        Game.currentLevel++;
        console.log("下一关");
      } else {
        Game.gameFrameIndex = 0;
        Game.currentLevel = 0;
        Game.levels = JSON.parse(JSON.stringify(levels));
        console.log("通关了，从第一关开始重复");
      }
    }
  }

  //   console.log(Game.currentLevel + 1, Game.levels.length);
  //   if (Game.currentLevel + 1 > Game.levels.length) {
  //     // 重复第一关
  //     Game.gameFrameIndex = 0;
  //     Game.levels = JSON.parse(JSON.stringify(levels));
  //     Game.currentLevel = 0;
  //   }

  敌人渲染器.render();
}

function start() {
  Game.players = [player1, player2];
  Game.init();

  const run = function () {
    Game.ctx.clearRect(0, 0, Game.width, Game.height);
    playersRender();
    cpusRender();
    map.move();
    map.render();
    Game.update();
    requestAnimationFrame(run);
  };
  run();
}

start();

// const div = document.createElement('div')
// div.style.width ="50px"

// document.querySelector("#main").appendChild(div)
